//
//  LookupCell.m
//
//  Created by lee jory on 09-10-22.
//  Copyright 2009 Netgen. All rights reserved.
//

#import "LookupCell.h"
#import "MCFunction.h"
#import "EGORefreshTableHeaderView.h"

@implementation LookupDataGridComponentDataSource
@synthesize titles,data,fieldTypes,columnWidth;
@end

@implementation LookupDataGridScrollView
@synthesize lookupCell;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *t = [touches anyObject];
	if([t tapCount] == 1){
		LookupCell *d = (LookupCell*)lookupCell;
		int idx = [t locationInView:self].y / d.cellHeight;
		[UIView beginAnimations:nil context:nil];
		[UIView setAnimationDuration:0.65];
		for(int i=0;i<[d.dataSource.titles count];i++){
			UILabel *l = (UILabel*)[lookupCell viewWithTag:idx * d.cellHeight + i + 1000];
			UILabel *lSpaceLeft = (UILabel*)[lookupCell viewWithTag:idx * d.cellHeight + i + 10000];
			UILabel *lSpaceRight = (UILabel*)[lookupCell viewWithTag:idx * d.cellHeight + i + 20000];
			l.alpha = .5;
			lSpaceLeft.alpha = .5;
			lSpaceRight.alpha = .5;
		}
		for(int i=0;i<[d.dataSource.titles count];i++){
			UILabel *l = (UILabel*)[lookupCell viewWithTag:idx * d.cellHeight + i + 1000];
			UILabel *lSpaceLeft = (UILabel*)[lookupCell viewWithTag:idx * d.cellHeight + i + 10000];
			UILabel *lSpaceRight = (UILabel*)[lookupCell viewWithTag:idx * d.cellHeight + i + 20000];
			l.alpha = 1.0;
			lSpaceLeft.alpha = 1.0;
			lSpaceRight.alpha = 1.0;
		}	
		[UIView commitAnimations];
	}
}
@end

@interface LookupCell (Private)
//int actionSheetFlag;
//初始化各子视图
-(void)layoutSubView:(CGRect)aRect;
//用数据项填冲数据
-(void)fillData;
@end
@implementation LookupCell
@synthesize dataSource, cellHeight, vRight, vLeft;
@synthesize listData;
@synthesize isMultiSelect;
@synthesize labelBufferArray;
@synthesize valueBufferArray;
@synthesize viewController;
@synthesize aRecord;

- (float)cellTitleHeight
{
	return 40.0;
}

-(void)addRightArrow:(UIView*)view
{
}

- (id)initWithFrame:(CGRect)aRect data:(LookupDataGridComponentDataSource*)aDataSource 
					 valueBufferArray:(NSMutableArray *)valueArray 
					 labelBufferArray:(NSMutableArray *)labelArray
							 listData:(NSMutableArray *)datas
						isMultiSelect:(bool) multiSelect
							   record:(NSMutableDictionary *)record 
		                    totalPage:(int)totalPg 
		                  currentPage:(int)curPage {
	self = [super initWithFrame:aRect];
	if (valueBufferArray == nil) {
		valueBufferArray = [[NSMutableDictionary alloc]init];
		labelBufferArray = [[NSMutableDictionary alloc]init];
	}
	if(self != nil){
		self.valueBufferArray = valueArray;
		self.labelBufferArray = labelArray;
		isMultiSelect = multiSelect;
		self.listData = datas;
		self.aRecord = record;
		self.clipsToBounds = YES;
		self.backgroundColor = [UIColor clearColor];
		self.dataSource = aDataSource;
		totalPage = totalPg;
		page = curPage;
		
		//初始显示视图及Cell的长宽高
		contentWidth = .0;
		cellHeight = 40.0;
		
		cellWidth = [[dataSource.columnWidth objectAtIndex:0] floatValue];
		for(int i = 1; i < [dataSource.columnWidth count]; i++) {
			contentWidth += [[dataSource.columnWidth objectAtIndex:i] floatValue];
		}
		
		int count = [dataSource.columnWidth count];
		if (contentWidth + cellWidth < CGRectGetWidth(aRect) && count > 1) {
			float other = (int)(CGRectGetWidth(aRect) - contentWidth - cellWidth) % (count-1);
			float addition = (CGRectGetWidth(aRect) - contentWidth - cellWidth - other) / (count-1);
			for(int i = 1;i < count;i++) {
				int columnWidth = [[dataSource.columnWidth objectAtIndex:i] intValue];
				if (i == count - 1) {
					[dataSource.columnWidth replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%f", columnWidth+addition+other]];
				}else {
					[dataSource.columnWidth replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%f", columnWidth+addition]];
				}
			}
			contentWidth = CGRectGetWidth(aRect)-cellWidth;
		}
		
		contentHeight = [dataSource.data count] * cellHeight;		//初始化各视图
		[self layoutSubView:aRect];
		
		//填冲数据
		[self fillData];

	}
	return self;
}
-(void)layoutSubView:(CGRect)aRect{
	vLeftContent = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(aRect) - cellWidth, 0, cellWidth, contentHeight)];
	vRightContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aRect.size.width - cellWidth, contentHeight)];
	vRightContent.backgroundColor = [UIColor colorWithRed:0.667 green:0.722 blue:0.788 alpha:1.000];
	vLeftContent.backgroundColor = [UIColor colorWithRed:0.667 green:0.722 blue:0.788 alpha:1.000];
	
	vLeftContent.opaque = YES;
	vRightContent.opaque = YES;

	vTopLeft = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(aRect) - cellWidth, 0, cellWidth, self.cellTitleHeight)];
	vTopRight = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aRect.size.width - cellWidth, self.cellTitleHeight)];
	
	vLeft = [[LookupDataGridScrollView alloc] initWithFrame:CGRectMake(0, self.cellTitleHeight,  CGRectGetWidth(aRect), aRect.size.height - self.cellTitleHeight)];
	vRight = [[LookupDataGridScrollView alloc] initWithFrame:CGRectMake(0, 0,  CGRectGetWidth(aRect) - cellWidth, contentHeight)];
	
	vLeft.backgroundColor = [UIColor colorWithRed:0.667 green:0.722 blue:0.788 alpha:1.000];
		
	vLeft.lookupCell = self;
	vRight.lookupCell = self;
	
	vLeft.opaque = YES;
	vRight.opaque = YES;
	vTopLeft.opaque = YES;
	vTopRight.opaque = YES;
	
	//设置ScrollView的显示内容
	vLeft.contentSize = CGSizeMake( CGRectGetWidth(aRect), contentHeight);
	vRight.contentSize = CGSizeMake(contentWidth, aRect.size.height - self.cellTitleHeight);
	
	//设置ScrollView参数
	vRight.delegate = self;	
	MCUserDefaults *defaults = [MCUserDefaults instance];
	vTopRight.backgroundColor = defaults.skin_bgListLabelSpaceColor;		
	vRight.backgroundColor = [UIColor clearColor];
	vTopLeft.backgroundColor = [UIColor colorWithRed:0.667 green:0.722 blue:0.788 alpha:1.000];
	vLeft.backgroundColor = [UIColor clearColor];
	
	//添加各视图
	[vRight addSubview:vRightContent];
	[vRightContent release];
	[vLeft addSubview:vLeftContent];
	[vLeftContent release];
	[vLeft addSubview:vRight];
	[vRight release];
	[self addSubview:vTopLeft];
	[vTopLeft release];
	[self addSubview:vLeft];
	[vLeft release];
	
	[vLeft bringSubviewToFront:vRight];
	[self addSubview:vTopRight];
	[vTopRight release];
	[self bringSubviewToFront:vTopLeft];	
}


-(void)fillData{
	float columnOffset = 0.0;
	float spaceWidth = 5.0;
	float lineWidth = 1.0;
	float lineHeight = 1.0;
	//填冲标题数据
	for(int column = 0; column < [dataSource.titles count]; column++){
		float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(columnOffset, 0, columnWidth - lineWidth, self.cellTitleHeight)];
		label.font = [UIFont systemFontOfSize:16.0f];
		NSArray *values = [[dataSource.titles objectAtIndex:column] componentsSeparatedByString:@":-:"];
		label.text = [values objectAtIndex:0];
		label.textColor = [UIColor whiteColor];
		if ([values count] > 1) {
			label.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [values objectAtIndex:1]]];
		}
		MCUserDefaults *defaults = [MCUserDefaults instance];
		label.backgroundColor = [UIColor colorWithPatternImage:defaults.skin_bgListLabelImg];
		label.textAlignment = UITextAlignmentCenter;
		
		if(0 == column){
			label.frame = CGRectMake(columnOffset, 0, columnWidth, self.cellTitleHeight);
			[vTopLeft addSubview:label];
		}
		else{	
			[vTopRight addSubview:label];
			columnOffset += columnWidth;
		}
		[label release];
	}	
	
	//填冲数据内容	
	for(int i = 0;i < [dataSource.data count]; i++){
		NSArray *rowData = [dataSource.data objectAtIndex:i];
		columnOffset = 0.0;
		MCUserDefaults *defaults = [MCUserDefaults instance];
		for(int column = 0; column < [rowData count]; column++){
			if (column == 0) {
				float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];
				UIButton *button = [[UIButton alloc] init];
				button.frame = CGRectMake(columnOffset, i * cellHeight, columnWidth - lineWidth, cellHeight - lineHeight);
				[button setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
				
				NSMutableDictionary *record = [self.listData objectAtIndex:i];
				if (isMultiSelect) {
					for (int j = 0; j < [valueBufferArray count]; j++) {
						NSString *key = [valueBufferArray objectAtIndex:j];
						if ([[record objectForKey:@"key"] isEqualToString:key]) {
							[button setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
							break;
						}
					}
				}else {
					if ([valueBufferArray count]>0) {
						NSString *key = [valueBufferArray objectAtIndex:0];
						if ([[record objectForKey:@"key"] isEqualToString:key]) {
							[button setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
						}
					}
				}		
				if (i % 2) {
					button.backgroundColor = defaults.skin_bgTableEvenColor;
				}
				else {
					button.backgroundColor = defaults.skin_bgTableOddColor;
				}
				button.tag = i;
				
				[button addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
				
				[vLeftContent addSubview:button];	
				[button release];
			} else {
				float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];;
				NSString *fieldType = [dataSource.fieldTypes objectAtIndex:column - 1];
				if ([fieldType isEqualToString:@"H"]
					||[fieldType isEqualToString:@"I"]
					||[fieldType isEqualToString:@"K"]
					||[fieldType isEqualToString:@"L"]) {
					NSArray *selectIcons = [[rowData objectAtIndex:column] componentsSeparatedByString:@"::"];
					if ([selectIcons count] > 1) {
						NSString *iconLabel = [selectIcons objectAtIndex:1];
						NSArray *icons = [iconLabel componentsSeparatedByString:@","];
						UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(columnOffset, i * cellHeight, columnWidth - lineWidth, cellHeight - lineHeight)];
						if (i % 2) {
							labelView.backgroundColor = defaults.skin_bgTableEvenColor;
						}
						else {
							labelView.backgroundColor = defaults.skin_bgTableOddColor;
						}
						labelView.tag = i * cellHeight + column + 1000;
						for (int j = 0; j < [icons count]; j++) {
							NSString *icon = [icons objectAtIndex:j];
							UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(28 * j + 5+([icons count]==1?(80-24)/2-5:0), 8, 24, 24)];
							imageView.image = [MCFunction scaleAndRotateImage:[UIImage imageNamed:icon] width:24.0 height:24.0];
							imageView.backgroundColor = [UIColor clearColor];
							[labelView addSubview:imageView];
							[imageView release];
						}
						[vRightContent addSubview:labelView];
						[labelView release];
						columnOffset += columnWidth;
						continue;
					}
				}
				UILabel *labelSpaceLeft = [[UILabel alloc] init];
				UILabel *labelSpaceRight = [[UILabel alloc] init];
				UILabel *label = [[UILabel alloc] init];
				labelSpaceLeft.frame = CGRectMake(columnOffset, i * cellHeight, spaceWidth, cellHeight - lineHeight);
				label.frame = CGRectMake(columnOffset + spaceWidth, i * cellHeight, columnWidth - spaceWidth*2 - lineWidth, cellHeight - lineHeight);
				labelSpaceRight.frame = CGRectMake(columnOffset + columnWidth - spaceWidth - lineWidth, i * cellHeight, spaceWidth, cellHeight - lineHeight);
				if ([fieldType isEqualToString:@"6"]
					||[fieldType isEqualToString:@"7"]
					||[fieldType isEqualToString:@"E"]
					||[fieldType isEqualToString:@"1"]){
					label.textAlignment = UITextAlignmentRight;
				}else {
					label.textAlignment = UITextAlignmentLeft;
					label.numberOfLines = 0;
				}
				label.font = [UIFont systemFontOfSize:16.0f];
				[label setLineBreakMode:UILineBreakModeTailTruncation];
				NSArray *values = [[rowData objectAtIndex:column] componentsSeparatedByString:@":-:"];
				label.text = [values objectAtIndex:0];
				if ([values count] > 1) {
					label.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [values objectAtIndex:1]]];
				}
				if (i % 2) {
					label.backgroundColor = defaults.skin_bgTableEvenColor;
					labelSpaceLeft.backgroundColor = defaults.skin_bgTableEvenColor;
					labelSpaceRight.backgroundColor = defaults.skin_bgTableEvenColor;
				}
				else {
					label.backgroundColor = defaults.skin_bgTableOddColor;
					labelSpaceLeft.backgroundColor = defaults.skin_bgTableOddColor;
					labelSpaceRight.backgroundColor = defaults.skin_bgTableOddColor;
				}				
				label.tag = i * cellHeight + column + 1000;
				labelSpaceLeft.tag = i * cellHeight + column + 10000;
				labelSpaceRight.tag = i * cellHeight + column + 20000;
				
				if(0 == column){
					label.frame = CGRectMake(columnOffset,  i * cellHeight , columnWidth - lineWidth, cellHeight - lineHeight);
					[vLeftContent addSubview:label];
				}
				else{	
					[vRightContent addSubview:labelSpaceLeft];
					[vRightContent addSubview:label];
					[vRightContent addSubview:labelSpaceRight];
					columnOffset += columnWidth;
				}
				
				if (column == [rowData count]-1 && column == 0) {
					[self addRightArrow:label];
				}
				[labelSpaceLeft release];
				[label release];
				[labelSpaceRight release];
			}
		}
	}	
}


//-------------------------------以下为事件触发方法----------------------------------------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
	if (scrollView == vRight) {
		vTopRight.frame = CGRectMake(0, 0, vRight.contentSize.width, vTopRight.frame.size.height);
		vTopRight.bounds = CGRectMake(scrollView.contentOffset.x, 0, vTopRight.frame.size.width, vTopRight.frame.size.height);
		vTopRight.clipsToBounds = YES;	
		vRightContent.frame = CGRectMake(0, 0, vRight.contentSize.width, contentHeight);
		[self addSubview:vTopRight];
		[self bringSubviewToFront:vTopLeft];
		vRight.frame =CGRectMake(0, 0, self.frame.size.width - cellWidth, vLeft.contentSize.height); 
		[vLeft addSubview:scrollView];
	}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
	if (scrollView == vRight) {
		scrollView.frame = CGRectMake(0, 0, scrollView.frame.size.width, self.frame.size.height);
		vRightContent.frame = CGRectMake(0, self.cellTitleHeight - vLeft.contentOffset.y, vRight.contentSize.width, contentHeight);
		
		vTopRight.frame = CGRectMake(0, 0, vRight.contentSize.width, vTopRight.frame.size.height);
		vTopRight.bounds = CGRectMake(0, 0, vRight.contentSize.width, vTopRight.frame.size.height);
		[scrollView addSubview:vTopRight];
		[self addSubview:scrollView];
	}
}
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//	if(!decelerate)
//		[self scrollViewDidEndDecelerating:scrollView];
//	if (self.totalPage > 1 && self.page < self.totalPage) {
//		if (refreshHeaderView == nil && scrollView == vLeft) {
//			CGRect frame = CGRectMake(0.0f, vLeft.contentSize.height, vLeft.bounds.size.width, 65);
//			refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:frame withPage:self.page withTotlePage:self.totalPage  withFlag:YES];
//			refreshHeaderView.backgroundColor = [UIColor clearColor];
//			[vLeft addSubview:refreshHeaderView];
//			[refreshHeaderView release];
//		}
//	}
//	int nScrollHeight = scrollView.bounds.size.height;
//	if (scrollView == vLeft  && !_reloading && refreshHeaderView &&
//		scrollView.contentOffset.y+nScrollHeight >= scrollView.contentSize.height+ 65.0f &&
//		[viewController getNextData]) {
//		_reloading = YES;
//		[refreshHeaderView setState:EGOOPullRefreshLoading];
//		[UIView beginAnimations:nil context:NULL];
//		[UIView setAnimationDuration:0.2];
//		scrollView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 60.0f, 0.0f);
//		[UIView commitAnimations];
//	}
//}
//
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{	
//	
//	if (scrollView.isDragging) {
//		int nScrollHeight = scrollView.bounds.size.height;
//		if (refreshHeaderView.state == EGOOPullRefreshPulling && refreshHeaderView &&
//			scrollView.contentOffset.y+nScrollHeight >= vLeft.contentSize.height && 
//			scrollView.contentOffset.y+nScrollHeight < vLeft.contentSize.height+65 && !_reloading) {
//			[refreshHeaderView setState:EGOOPullRefreshNormal];
//		} else if (refreshHeaderView.state == EGOOPullRefreshNormal && refreshHeaderView &&
//				   scrollView.contentOffset.y+nScrollHeight >= vLeft.contentSize.height+65 && !_reloading) {
//			[refreshHeaderView setState:EGOOPullRefreshPulling];
//		}
//	}
//}
//
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	if(!decelerate)
		[self scrollViewDidEndDecelerating:scrollView];
	if (totalPage > 1 && page < totalPage) {
		if (refreshHeaderView == nil && scrollView == vLeft) {
			CGRect frame = CGRectMake(0.0f, vLeft.contentSize.height, vLeft.bounds.size.width, 65);
			refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:frame withPage:page withTotlePage:totalPage withFlag:YES];
			refreshHeaderView.backgroundColor = [UIColor clearColor];
			[vLeft addSubview:refreshHeaderView];
			[refreshHeaderView release];
		}
	}
	int nScrollHeight = scrollView.bounds.size.height;
	if (scrollView == vLeft  && !_reloading && refreshHeaderView &&
		scrollView.contentOffset.y+nScrollHeight >= scrollView.contentSize.height+ 65.0f &&
		[viewController respondsToSelector:@selector(getNextData)] &&
		[viewController performSelector:@selector(getNextData)]) {
		_reloading = YES;
		[refreshHeaderView setState:EGOOPullRefreshLoading];
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.2];
		scrollView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 60.0f, 0.0f);
		[UIView commitAnimations];
	}
	
	if ([MCUserDefaults instance].dragListLastAndMore) {
		if (totalPage > 1 && page > 0) {
			if (refreshHeaderView_Up == nil && scrollView == vLeft) {
				CGRect frame = CGRectMake(0.0f, -65, vLeft.bounds.size.width, 65.0);
				refreshHeaderView_Up = [[EGORefreshTableHeaderView alloc] initWithFrame:frame withPage:page withTotlePage:totalPage withFlag:NO];
				refreshHeaderView_Up.backgroundColor = [UIColor clearColor];
				[vLeft addSubview:refreshHeaderView_Up];
				[refreshHeaderView_Up release];
			}
		}
		if (scrollView.contentOffset.y <= - 65.0f && !_reloading_up && 
			[viewController respondsToSelector:@selector(getPreviousData)] &&
			[viewController performSelector:@selector(getPreviousData)]) {
			_reloading_up = YES;
			[refreshHeaderView_Up setState:EGOOPullRefreshLoading];
			[UIView beginAnimations:nil context:NULL];
			[UIView setAnimationDuration:0.2];
			scrollView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
			[UIView commitAnimations];
		}
	}
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{	
	
	if (scrollView.isDragging) {
		int nScrollHeight = scrollView.bounds.size.height;
		if (refreshHeaderView.state == EGOOPullRefreshPulling && refreshHeaderView &&
			scrollView.contentOffset.y+nScrollHeight >= vLeft.contentSize.height && 
			scrollView.contentOffset.y+nScrollHeight < vLeft.contentSize.height+65 && !_reloading) {
			[refreshHeaderView setState:EGOOPullRefreshNormal];
		} else if (refreshHeaderView.state == EGOOPullRefreshNormal && refreshHeaderView &&
				   scrollView.contentOffset.y+nScrollHeight >= vLeft.contentSize.height+65 && !_reloading) {
			[refreshHeaderView setState:EGOOPullRefreshPulling];
		}
		
		if ([MCUserDefaults instance].dragListLastAndMore) {
			if (refreshHeaderView_Up.state == EGOOPullRefreshPulling && scrollView.contentOffset.y > -65.0f && scrollView.contentOffset.y < 0.0f && !_reloading_up) {
				[refreshHeaderView_Up setState:EGOOPullRefreshNormal];
			} else if (refreshHeaderView_Up.state == EGOOPullRefreshNormal && scrollView.contentOffset.y < -65.0f && !_reloading_up) {
				[refreshHeaderView_Up setState:EGOOPullRefreshPulling];
			}
		}
	}
}

- (void)refreshFinished:(int)curPage {
	
	_reloading = NO;
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:.3];
	[vLeft setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
	[UIView commitAnimations];
	
	[refreshHeaderView setState:EGOOPullRefreshNormal];
	
	if ([MCUserDefaults instance].dragListLastAndMore) {
		_reloading_up = NO;
		[refreshHeaderView_Up setState:EGOOPullRefreshNormal];
	}
}


- (void) dealloc {
	vLeft == nil;
	vRight == nil;
	vRightContent == nil;
	vLeftContent == nil;
	vTopLeft == nil;
	vTopRight == nil;
	refreshHeaderView = nil;
	refreshHeaderView_Up = nil;
	[listData release];
	[labelBufferArray release];
	[valueBufferArray release];
	[aRecord release];
	[super dealloc];
}

-(void)doAction:(id)inSender {
	UIButton *button = (UIButton *)inSender;
	int row = button.tag;	
	NSMutableDictionary *record = [self.listData objectAtIndex:row];
	if (isMultiSelect) {
		bool selectFlag = YES;
		for (int i = 0; i < [valueBufferArray count]; i++) {
			NSString *key = [valueBufferArray objectAtIndex:i];
			if ([[record objectForKey:@"key"] isEqualToString:key]) {
				[valueBufferArray removeObjectAtIndex:i];
				[labelBufferArray removeObjectAtIndex:i];
				selectFlag = NO;
				break;
			}
		}
		if (selectFlag) {
			[button setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
			[valueBufferArray addObject:[record objectForKey:@"key"] == nil?@"":[record objectForKey:@"key"]];
			[labelBufferArray addObject:[record objectForKey:@"desc"] == nil?@"":[record objectForKey:@"desc"]];
		}else {
			[button setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
		}	
	}else {
		
		if ([valueBufferArray count] > 0) {
			if ([[valueBufferArray objectAtIndex:0] isEqualToString:[record objectForKey:@"key"]]) {
				[button setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
				[valueBufferArray removeObjectAtIndex:0];
				if ([labelBufferArray count] > 0) {
					[labelBufferArray removeObjectAtIndex:0];
				}
			} else {
				for (UIButton *button in vLeftContent.subviews) {
					[button setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal]; 
				}
				[button setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
				[valueBufferArray removeObjectAtIndex:0];
				if ([labelBufferArray count] > 0) {
					[labelBufferArray removeObjectAtIndex:0];
				}
				[valueBufferArray addObject:[record objectForKey:@"key"] == nil?@"":[record objectForKey:@"key"]];
				[labelBufferArray addObject:[record objectForKey:@"desc"] == nil?@"":[record objectForKey:@"desc"]];
			}
		}else {
			[button setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
			[valueBufferArray addObject:[record objectForKey:@"key"] == nil?@"":[record objectForKey:@"key"]];
			[labelBufferArray addObject:[record objectForKey:@"desc"] == nil?@"":[record objectForKey:@"desc"]];
		}
		self.aRecord = record;
	}
}


@end
